/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.dinuscxj.circleprogressbar.slice;

import com.dinuscxj.circleprogressbar.ResourceTable;
import com.dinuscxj.progressbar.CircleProgressBar;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;
import ohos.eventhandler.InnerEvent;

/**
 * 展示页面 MainAbilitySlice
 */
public class MainAbilitySlice extends AbilitySlice {
    private CircleProgressBar mLineProgressBar;
    private CircleProgressBar mSolidProgressBar;
    private CircleProgressBar mCustomProgressBar1;
    private CircleProgressBar mCustomProgressBar2;
    private CircleProgressBar mCustomProgressBar3;
    private CircleProgressBar mCustomProgressBar4;
    private CircleProgressBar mCustomProgressBar5;
    private CircleProgressBar mCustomProgressBar6;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_test_main);
        mLineProgressBar = (CircleProgressBar) findComponentById(ResourceTable.Id_line_progress);
        mSolidProgressBar = (CircleProgressBar) findComponentById(ResourceTable.Id_solid_progress);
        mCustomProgressBar1 = (CircleProgressBar) findComponentById(ResourceTable.Id_custom_progress1);
        mCustomProgressBar2 = (CircleProgressBar) findComponentById(ResourceTable.Id_custom_progress2);
        mCustomProgressBar3 = (CircleProgressBar) findComponentById(ResourceTable.Id_custom_progress3);
        mCustomProgressBar4 = (CircleProgressBar) findComponentById(ResourceTable.Id_custom_progress4);
        mCustomProgressBar5 = (CircleProgressBar) findComponentById(ResourceTable.Id_custom_progress5);
        mCustomProgressBar6 = (CircleProgressBar) findComponentById(ResourceTable.Id_custom_progress6);


        mCustomProgressBar4.setProgressFormatter(new CircleProgressBar.ProgressFormatter(){

            @Override
            public CharSequence format(int progress, int max) {
                return progress + "s";
            }
        });
        mCustomProgressBar6.setProgressFormatter(null);
        simulateProgress();
    }

    private void simulateProgress() {
        EventHandler handler = new EventHandler(EventRunner.getMainEventRunner()) {
            @Override
            protected void processEvent(InnerEvent event) {
            }
        };
        Runnable runnable1 = new Runnable() {
            @Override
            public void run() {
                if (mCount == Integer.MAX_VALUE) {
                    mCount = 0;
                }
                setProgressValue(mCount % 100);
                mCount++;
                handler.postTask(this, 100);
            }
        };
        handler.postTask(runnable1, 100);

    }

    private int mCount = 0;

    private void setProgressValue(int progress) {
        mLineProgressBar.setProgress(progress);
        mSolidProgressBar.setProgress(progress);
        mCustomProgressBar1.setProgress(progress);
        mCustomProgressBar2.setProgress(progress);
        mCustomProgressBar3.setProgress(progress);
        mCustomProgressBar4.setProgress(progress);
        mCustomProgressBar5.setProgress(progress);
        mCustomProgressBar6.setProgress(progress);
    }


    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }


}
